package com.estore.view;

import com.estore.model.Product;
import com.estore.service.ProductService;

import java.util.Scanner;

public class ConsoleShop {
    private static final Scanner sc = new Scanner(System.in);
    private static final ProductService productService = new ProductService();

    public static void main(String[] args) {
        while (true) {
            menu();
            int opt = sc.nextInt();
            sc.nextLine();
            switch (opt) {
                case 1:
                    productService.listProducts().forEach(System.out::println);
                    break;
                case 2:
                    System.out.print("ID 名称 价格：");
                    String[] arr = sc.nextLine().split(" ");
                    productService.addProduct(new Product(Integer.parseInt(arr[0]), arr[1], Double.parseDouble(arr[2])));
                    break;
                case 3:
                    System.out.print("删除ID：");
                    productService.removeProduct(sc.nextInt());
                    break;
                case 99:
                    productService.backupData();
                    break;
                case 0:
                    productService.saveData();
                    System.out.println("已退出");
                    return;
                default:
                    System.out.println("无效选项");
            }
        }
    }

    private static void menu() {
        System.out.println("=== 简易商城 ===");
        System.out.println("1. 列表  2. 新增  3. 删除  0. 退出");
        System.out.print("请选择：");
    }
}